package com.hb.demo

import io.reactivex.Observable
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

fun test01():Observable<String>{
    Thread.sleep(1000)
    return Observable.just("test01")
}

fun test02():Observable<String>{
    Thread.sleep(1000)
    return Observable.just("test02")
}

fun complete(){
    Thread.sleep(1000)
    println("complete")
}

fun main() {
    val threadCount=Runtime.getRuntime().availableProcessors()
    //线程池
    val threadPoolExecutor= Executors.newFixedThreadPool(threadCount)
    //调度器
    val scheduler = Schedulers.from(threadPoolExecutor)

    val start = System.currentTimeMillis()
    Observable.merge(test01().subscribeOn(scheduler), test02().subscribeOn(scheduler)).subscribe(::println,::error, ::complete)
    Thread.sleep(1000)
    val end = System.currentTimeMillis()


    val toSeconds = TimeUnit.MILLISECONDS.toSeconds(end - start)

    println(toSeconds)
}